package com.zmc.member.infrastructure.component.collection.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.redis.RedisCache;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.member.infrastructure.bean.query.MyCollectionQuery;
import com.zmc.member.infrastructure.bean.vo.MyCollectionVo;
import com.zmc.member.infrastructure.component.collection.MyCollectionStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author：zmc
 * @Date：2025/5/27 11:25
 */
@Component
public class MyCollectionCacheServiceImpl implements MyCollectionStrategyService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    public PageUtils pageList(MyCollectionQuery query) {
        List<String> list = redisCache.getCacheList(authTokenService.getMemberInfo().getMemberId());
        return new PageUtils(list, (long) list.size(), (long)query.getPageSize(), (long)query.getPageNum());
    }

    @Override
    public void save(MyCollectionVo vo) {
        if (!redisCache.listContainsValue(authTokenService.getMemberInfo().getMemberId(), vo.getSkuId())) {
            redisCache.setCacheList(authTokenService.getMemberInfo().getMemberId(), vo.getSkuId());
        }
    }

    @Override
    public void remove(MyCollectionVo vo) {
        if (redisCache.listContainsValue(authTokenService.getMemberInfo().getMemberId(), vo.getSkuId())) {
            redisCache.setCacheList(authTokenService.getMemberInfo().getMemberId(), vo.getSkuId());
        }
    }

    @Override
    public boolean contains(String skuId) {
        return redisCache.listContainsValue(authTokenService.getMemberInfo().getMemberId(), skuId);
    }
}
